/**
 * Given two strings,write a method to decide if one is a permutation of the other.
 * <p>
 * <p>
 * Example 1:
 * <p>
 * <p>
 * Input: s1 = "abc", s2 = "bca"
 * Output: true
 * <p>
 * <p>
 * Example 2:
 * <p>
 * <p>
 * Input: s1 = "abc", s2 = "bad"
 * Output: false
 * <p>
 * <p>
 * Note:
 * <p>
 * <p>
 * 0 <= len(s1) <= 100
 * 0 <= len(s2) <= 100
 * <p>
 * <p>
 * Related Topics 哈希表 字符串 排序 👍 141 👎 0
 */


package com.xixi.basicAlgroithms.hash;

import java.util.HashMap;
import java.util.Map;

public class ID_interview_01_02_CheckPermutationLcci {
    public static void main(String[] args) {
        Solution solution = new ID_interview_01_02_CheckPermutationLcci().new Solution();
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {


        public boolean CheckPermutation(String s1, String s2) {

            Map<Character, Integer> s1Map = new HashMap<>();

            char[] s1Char = s1.toCharArray();

            for (Character c1 : s1Char) {
                s1Map.put(c1, s1Map.getOrDefault(c1, 0) + 1);
            }
            char[] s2Char = s2.toCharArray();
            for (Character c2 : s2Char) {
                if (!s1Map.containsKey(c2)) return false;
                if (s1Map.get(c2) == 0) return false;
                int nowCount = s1Map.get(c2);
                if (nowCount == 1) {
                    s1Map.remove(c2);
                } else {
                    s1Map.put(c2, nowCount - 1);
                }
            }


            return s1Map.isEmpty();

        }
    }
//leetcode submit region end(Prohibit modification and deletion)


}